package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.InverterAlarmDetails;
import com.base.vistter.iframe.entity.InverterDevices;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.InverterAlarmDetailsService;
import com.base.vistter.iframe.service.InverterDevicesService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/devices")
public class InverterDevicesController {
    @Resource(name = "inverterDevicesService")
    private InverterDevicesService inverterDevicesService;
    @Resource(name = "inverterAlarmDetailsService")
    private InverterAlarmDetailsService inverterAlarmDetailsService;

    @GetMapping
    public Result<Page<InverterDevices>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, Integer stationId, Integer status) throws IframeException {
        Page<InverterDevices> page = Page.of(pageNo, pageNum);
        return Result.ok(inverterDevicesService.pageInverterDevices(page , stationId, status));
    }

    @GetMapping("/search")
    public Result<InverterDevices> search(String no) throws IframeException {
        LambdaQueryWrapper<InverterDevices> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(InverterDevices::getNo, no).last("LIMIT 1");
        return Result.ok(inverterDevicesService.selectOne(lambdaQueryWrapper));
    }

    @GetMapping("/info")
    public Result<InverterDevices> get(Integer id, Integer alarmId) throws IframeException {
        if (id != null) {
            return Result.ok(inverterDevicesService.getById(id));
        } else {
            InverterAlarmDetails inverterAlarmDetails = inverterAlarmDetailsService.getById(alarmId);
            return Result.ok(inverterDevicesService.getById(inverterAlarmDetails.getInverterDevicesId()));
        }
    }

}
